﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Entity;
namespace QuanLyPhongGiam
{
    public partial class frmDSSV : Form
    {
        public frmDSSV()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            List<BiCan> lstBiCan = Query.GetAllBiCans();
            LoadListBiCan(lstBiCan);
        }
        private void LoadListBiCan(List<BiCan> lstBiCan)
        {
            foreach (var biCan in lstBiCan)
            {
                ListViewItem item = new ListViewItem(biCan.MaBiCan);
                item.SubItems.Add(biCan.TenBiCan);
                if (biCan.GioiTinh != null)
                {
                    if (Convert.ToInt16(biCan.GioiTinh) == 1)
                        item.SubItems.Add("Nam");
                    else
                        item.SubItems.Add("Nu");
                }
                item.SubItems.Add(biCan.NgaySinh.ToString());
                item.SubItems.Add(biCan.NoiSinh);
                if (biCan.TruongHop == 5)
                {
                    item.SubItems.Add("");
                }
                else
                {
                    TruongHopTamGiam th = Query.GetTruongHopById(biCan.TruongHop);
                    item.SubItems.Add(th.TenTruongHop);
                }
                //---------------------------------Giai doan dieu tra------------------------------//
                //if (biCan.DT_TuNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.DT_TuNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                //if (biCan.DT_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.DT_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                //if (biCan.DT_1_TuNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.DT_1_TuNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                //if (biCan.DT_1_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.DT_1_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                //if (biCan.DT_2_TuNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.DT_2_TuNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                //if (biCan.DT_2_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.DT_2_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                //if (biCan.DT_3_TuNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.DT_3_TuNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                //if (biCan.DT_3_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.DT_3_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                //if (biCan.DT_DacBiet_TuNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.DT_DacBiet_TuNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                //if (biCan.DT_DacBiet_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.DT_DacBiet_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                //if (biCan.DT_BoSung_TuNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.DT_BoSung_TuNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                //if (biCan.DT_BoSung_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.DT_BoSung_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                ////---------------------------------Giai doan truy to------------------------------//
                //if (biCan.TT_TuNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.TT_TuNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                //if (biCan.TT_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.TT_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                //if (biCan.TT_1_TuNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.TT_1_TuNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                //if (biCan.TT_1_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.TT_1_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                ////---------------------------------Giai doan xet xu------------------------------//
                //if (biCan.XX_TuNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.XX_TuNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                //if (biCan.XX_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.XX_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                //if (biCan.XX_1_TuNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.XX_1_TuNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                //if (biCan.XX_1_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.XX_1_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                ////---------------------------------Giai doan thi hanh an------------------------------//
                ////1
                //if (biCan.THA_TuNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.THA_TuNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                //if (biCan.THA_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.THA_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                ////2 thi hanh an ko phai la DEFAULT
                //if (biCan.GiaiQuyet_THA_HinhThuc == 15)
                //{
                //    item.SubItems.Add("");
                //}
                //else
                //{
                //    HinhThucGiaiQuyet th = Query.GetHinhThucGiaiQuyetById(biCan.GiaiQuyet_THA_HinhThuc);
                //    item.SubItems.Add(th.TenHinhThucGiaiQuyet);
                //}
                //if (biCan.GiaiQuyet_THA_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.GiaiQuyet_THA_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                ////3
                //if (biCan.ChuyenNoiKhac_Den != null)
                //    item.SubItems.Add(biCan.ChuyenNoiKhac_Den);
                //else
                //    item.SubItems.Add("");
                //if (biCan.ChuyenNoiKhac_Ngay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.ChuyenNoiKhac_Ngay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                ////---------------------------------Giai doan xet xu phuc tham------------------------------//
                ////4
                //if (biCan.XXPT_TuNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.XXPT_TuNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                //if (biCan.XXPT_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.XXPT_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                ////5
                //if (biCan.GiaiQuyet_THA_DenNgay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.GiaiQuyet_XXPT_DenNgay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");
                ////---------------------------------Giai doan khang cao khang nghi------------------------------//
                //if (biCan.KhangCao_Ngay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.KhangCao_Ngay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                //if (biCan.Tron_HinhThuc == 0)
                //{
                //    item.SubItems.Add("");
                //}
                //else
                //{
                //    item.SubItems.Add(biCan.Tron_HinhThuc.ToString());
                //}
                //if (biCan.Tron_Ngay != null)
                //    item.SubItems.Add(Convert.ToDateTime(biCan.Tron_Ngay).ToString("dd/MM/yyyy"));
                //else
                //    item.SubItems.Add("");

                listView1.Items.Add(item);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            frmSuaThongTinBiCan frmthem = new frmSuaThongTinBiCan(null, Query.insertNew);
            frmthem.Show();
            this.Close();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

        //public string listView1_MouseClick(object sender, MouseEventArgs e)
        //{

        //}

        private void listView1_Click(object sender, EventArgs e)
        {
            //string str;
            //int row = this.listView1.SelectedItems[0].Index;
            //str = this.listView1.Items[row].SubItems[0].Text;
            //frmsuaSV frm = new frmsuaSV(str, MaLop);


        }


        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            string biCan_ID;
            if (listView1.SelectedItems.Count > 0)
            {
                int row = this.listView1.SelectedItems[0].Index;
                biCan_ID = this.listView1.Items[row].SubItems[0].Text;
                frmSuaThongTinBiCan frm = new frmSuaThongTinBiCan(biCan_ID, Query.edit);
                frm.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("VUI LÒNG CHỌN", "THÔNG BÁO");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string biCan_ID;
            if (listView1.SelectedItems.Count > 0)
            {
                int row = this.listView1.SelectedItems[0].Index;
                biCan_ID = this.listView1.Items[row].SubItems[0].Text;
                using (var db = new QuanLySinhVienEntities())
                {
                    var biCan = (from s1 in db.BiCans
                                 where s1.MaBiCan == biCan_ID
                                 select s1).FirstOrDefault();

                    if (biCan != null)
                    {
                        db.BiCans.Remove(biCan);
                        db.SaveChanges();
                        MessageBox.Show("BẠN ĐÃ XÓA THÀNH CÔNG", "THÔNG BÁO");
                        listView1.Items.Clear();
                        List<BiCan> lstBiCan = Query.GetAllBiCans();
                        LoadListBiCan(lstBiCan);
                    }
                }
            }
            else
            {
                MessageBox.Show("VUI LÒNG CHỌN", "THÔNG BÁO");
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            string tenBiCan = txtTenBiCanTim.Text;
            using (var db = new QuanLySinhVienEntities())
            {
                var biCan = (from bican in db.BiCans
                             where bican.TenBiCan.Contains(tenBiCan)
                             select bican).ToList();

                if (biCan != null)
                {
                    listView1.Items.Clear();
                    LoadListBiCan(biCan);
                }
            }
        }
        private void btnChiTiet_Click(object sender, EventArgs e)
        {
            string biCan_ID;
            if (listView1.SelectedItems.Count > 0)
            {
                int row = this.listView1.SelectedItems[0].Index;
                biCan_ID = this.listView1.Items[row].SubItems[0].Text;
                frmSuaThongTinBiCan frm = new frmSuaThongTinBiCan(biCan_ID, Query.detail);
                frm.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("VUI LÒNG CHỌN", "THÔNG BÁO");
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            List<BiCan> lstBiCan = Query.GetAllBiCans();
            LoadListBiCan(lstBiCan);
        }

    }
}
